﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

public class OutLog : MonoBehaviour
{
    private bool showlog;//是否显示 普通的日志

    public struct LogMsg
    {
        public LogType logType;
        public string msg;

        public LogMsg(LogType logType, string msg)
        {
            this.logType = logType;
            this.msg = msg;
        }
    }

    public int maxCount = 2000;
    private static List<LogMsg> mLines = new List<LogMsg>();
    private static List<string> mWriteTxt = new List<string>();
    private string outpath;
    private bool showing = true;

    private void Awake()
    {
        //Application.persistentDataPath Unity中只有这个路径是既可以读也可以写的。
        outpath = Application.persistentDataPath + "/outLog.txt";
        Debug.Log(Application.persistentDataPath);
        //每次启动客户端删除之前保存的Log
        if (System.IO.File.Exists(outpath))
        {
            File.Delete(outpath);
        }
        //在这里做一个Log的监听
        Application.logMessageReceived += HandleLog;
        //一个输出
    }

    private void Update()
    {
        //因为写入文件的操作必须在主线程中完成，所以在Update中哦给你写入文件。
        if (mWriteTxt.Count > 0)
        {
            string[] temp = mWriteTxt.ToArray();
            foreach (string t in temp)
            {
                using (StreamWriter writer = new StreamWriter(outpath, true, Encoding.UTF8))
                {
                    writer.WriteLine(t);
                }
                mWriteTxt.Remove(t);
            }
        }
    }

    private void HandleLog(string logString, string stackTrace, LogType type)
    {
        //忽略警告
        if (type == LogType.Warning)
        {
            return;
        }
        //写文件夹
        mWriteTxt.Add(logString);
        Log(new LogMsg(type, logString));

        if (type == LogType.Error || type == LogType.Exception)
        {
            Log(new LogMsg(type, stackTrace));
        }
    }

    //这里我把错误的信息保存起来，用来输出在手机屏幕上
    public void Log(LogMsg obj)
    {
        if (Application.isPlaying)
        {
            if (mLines.Count > maxCount)
            {
                mLines.RemoveAt(0);
            }
            mLines.Add(obj);
        }
    }

    private Vector2 scVector;
    private int i = 0;

    private void OnGUI()
    {
        GUILayout.BeginHorizontal();
        if (GUILayout.Button(showing ? "关闭Log" : "打开Log"))
        {
            showing = !showing;
        }

        if (GUILayout.Button(showlog ? "显示普通日志" : "关闭普通日志"))
        {
            showlog = !showlog;
        }
        if (GUILayout.Button("打开持久化路径"))
        {
            Application.OpenURL(Application.persistentDataPath);
        }
        GUILayout.EndHorizontal();

        if (showing)
        {
            scVector = GUILayout.BeginScrollView(scVector);

            for (int i = 0, imax = mLines.Count; i < imax; ++i)
            {
                if (mLines[i].logType == LogType.Log && !showlog) //跳过普通日志
                {
                    continue;
                }
                GUI.color = mLines[i].logType == LogType.Log ? Color.white : Color.red;

                GUILayout.Label(mLines[i].msg);
            }

            GUILayout.EndScrollView();
        }
    }
}